package org.gurms.web.action.system;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.gurms.common.util.HibernateWebUtils;
import org.gurms.common.validate.GurmsValid;
import org.gurms.service.dto.PropertyFilter;
import org.gurms.service.dto.system.UserDto;
import org.gurms.service.system.SysUserService;
import org.gurms.web.action.BaseAction;
import org.gurms.web.action.CRUDActionSupport;
import org.gurms.web.action.Struts2ActionUtil;
import org.springframework.beans.factory.annotation.Autowired;

@Results({
	@Result(name=BaseAction.RESULT_RELOAD, location="sys-user", type="redirect")
})
public class SysUserAction extends CRUDActionSupport<UserDto> {

	private UserDto user = new UserDto();
	@Autowired
	private SysUserService sysUserService;
	
	@GurmsValid(type=UserDto.class)
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

	@Override
	public String delete() throws Exception {
		return RESULT_RELOAD;
	}

	public String detail() throws Exception {
		// TODO Auto-generated method stub
		return RESULT_DETAIL;
	}
		
	public String save() throws Exception {
		sysUserService.save(user);
		return RESULT_RELOAD;
	}

	//---------------------ajax-------------------------------
	public String flexigrid(){
		System.out.println(Struts2ActionUtil.getRequest().getParameter("query"));
		System.out.println(Struts2ActionUtil.getRequest().getParameter("qtype"));
		List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(Struts2ActionUtil.getRequest());
		page = sysUserService.search(page, filters);
		return renderFlexigrid(page , "userid");
	}
	
	public String ajaxSave(){
		try{
			sysUserService.save(user);
		}catch(Exception e){
			logger.error("保存用户异常", e);
			page.setSuccess(false);
			page.setReturncode("9999");
			page.setReturnmsg("0000");
		}
		Struts2ActionUtil.renderJson(page);
		return NONE;
	}
	
	public String ajaxDetail(){
		try{
			UserDto dto = sysUserService.get(user);
			page.addResult(dto);
		}catch(Exception e){
			logger.error("查询用户异常", e);
			page.setSuccess(false);
			page.setReturncode("9999");
			page.setReturnmsg("0000");
		}
		Struts2ActionUtil.renderJson(page);
		return NONE;
	}
	
	//----------------------getter/setter---------------------
	public UserDto getModel() {
		return user;
	}

	public void setSysUserService(SysUserService userservice) {
		this.sysUserService = userservice;
	}
}
